<?php

namespace app\tenant\controller;

use think\Controller;
use Util\SysDb;
use think\Db;

class Article extends Controller
{
    public function Articlelist()
    {
      
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tenantid = trim(input('param.tenantid'));
        $tol = (intval($page) - 1) * $limit;
        $status = trim(input('param.status'));
        $type = trim(input('param.type'));
        $where=array(
            "status" => $status,
            "type" => $type,
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $list = DB::name("sys_article")->order("create_time desc")->where($where)->where(['tenant_id' =>  $tenantid])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('sys_article')->where($where)->where(['tenant_id' =>  $tenantid])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function Articledel()
    {
        $id = trim(input('param.id'));
        $admin = Db::name('sys_article')->where('id', $id)->delete();;
        if ($admin) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "删除成功"]));
        }
    }

    public function Articleupdatestatus()
    {
        $id = trim(input('param.id'));
        $status = trim(input('param.status'));
        $this->db = new SysDb();
        $data = $this->db->table('sys_article')->where('id', $id)->item();
        $data['status']=$status;
        $admin = $this->db->table('sys_article')->where(['id' =>  $id])->update($data);
        if ($admin) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "修改成功"]));
        }
    }

    public function Articledetail()
    {
        $id = trim(input('param.id'));
        $tenantid = trim(input('param.tenantid'));
        $data = DB::name("sys_article")->where(['id' => $id])->where(['tenant_id' =>  $tenantid])->find();
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" =>$data]));
    }

    public function Articleupload()
    {
        $img = trim(input('param.img'));
        $needle = 'base64'; //判断是否包含a这个字符
        $tmparray = explode($needle, $img);
        if (count($tmparray) > 1) {
            $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . '.png';
            if (strstr($img, ",")) {
                $image = explode(',', $img);
                $image = $image[1];
            }
            $path = "./uploads/" . date("Ymd", time());
            $paths = "http://127.0.0.1:8000/uploads/" . date("Ymd", time());
            if (!is_dir($path)) { //判断目录是否存在 不存在就创建
                mkdir($path, 0777, true);
            }
            $imageSrc = $path . "/" . $imageName; //图片名字
            $r = file_put_contents($imageSrc, base64_decode($image)); //返回的是字节数
            $imageSrcs = $paths . "/" . $imageName; //图片名字
            if (!$r) {
                exit(json_encode(['message' => 'error', 'result' => "失败"]));
            } else {
                $data=array();
                array_push($data,$imageSrcs);
                exit(json_encode(['code' => 1, 'message' => 'succ', "result" =>$data]));
            }
        } 
    }

    public function Articleupdate()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $data = $this->db->table('sys_article')->where(['id' =>  $id])->item();
        $data['type'] = trim(input('param.type'));
        $data['status'] = trim(input('param.status'));
        $data['title']= trim(input('param.title'));
        $data['summary'] = trim(input('param.summary'));
        $data['tenant_id'] = trim(input('param.tenantid'));
        $data['keywords'] = trim(input('param.keywords'));
        $data['content'] = trim(input('param.content'));
      
        $count = $this->db->table('sys_article')->where(['id' =>  $id])->update($data);
        if($count){
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" =>"修改成功"]));
        }
    }

    public function Articleadd()
    {
        $data['type'] = trim(input('param.type'));
        $data['status'] = trim(input('param.status'));
        $data['title']= trim(input('param.title'));
        $data['summary'] = trim(input('param.summary'));
        $data['tenant_id'] = trim(input('param.tenantid'));
        $data['keywords'] = trim(input('param.keywords'));
        $data['content'] = trim(input('param.content'));
        $this->db = new SysDb();
        $count = $this->db->table('sys_article')->insert($data);
        if($count){
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" =>"添加成功"]));
        }
    }
}